#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include<string.h>
#include "support.h"
#include "clist.h"
#include "head.h"
#include "newinfo.h"
#include "file.h"
#include "save.h"
#include "mainwindow.h"

struct clist_struct clist;

void clist_group_get_selection_row(GtkCList *gtkclist,gint row,gint column,
					GdkEventButton *event,gpointer user_data)
{
	struct newinfo_struct *newinfo=(struct newinfo_struct *)user_data;
	struct clist_struct *clist;
	struct file_list *flist;
	gint i,j;
	gint context_id;
	gchar text[80];
	#ifdef _DEBUG_
		printf("clist_group_get_selection_row:---------\n");
	#endif
	if(newinfo->clist != NULL) {
		flist=newinfo->global_file->files;
		clist=newinfo->clist;
		if(clist->group_selected_row != row) {
			if(newinfo->global_file->cur_file->modify == TRUE) {
				save_tmp_file(newinfo);
			}
			for(i=0;i<newinfo->global_file->group_nums && flist->next != NULL;i++) {
				if(flist->group_id == row) {
					newinfo->global_file->cur_file=flist;
					strncpy(newinfo->global_file->current,flist->name,FILE_NAME_LEN);
					newinfo->global_file->modify=TRUE;
					save_config_file(newinfo->global_file);
					printf("current name:%s\n",newinfo->global_file->current);
					break;
				}
				flist=flist->next;
			}
			gtk_clist_clear(GTK_CLIST(clist->clist_show));
			read_data_file_to_show(newinfo->global_file->tmp,newinfo->global_file,clist);
		}
	}
	clist->group_selected_row=row;
	clist->group_selected_column=column;
	
	g_snprintf(text,80,"选中分组: %d     选中信息: %d",
			clist->group_selected_row+1,clist->show_selected_row+1);
	context_id=gtk_statusbar_get_context_id(GTK_STATUSBAR(clist->statusbar),"");
	gtk_statusbar_push(GTK_STATUSBAR(clist->statusbar),context_id,text);
	#ifdef _DEBUG_
		printf("current group selected:(%d,%d)\n",clist->group_selected_row,
									clist->group_selected_column);
	#endif
}
					
void clist_show_get_selection_row(GtkCList *gtkclist,gint row,gint column,
					GdkEventButton *event,gpointer user_data)
{
	struct clist_struct *clist=(struct clist_struct *)user_data;
	gint context_id;
	gchar text[80];
	clist->show_selected_row=row;
	clist->show_selected_column=column;
	
	g_snprintf(text,80,"选中分组: %d     选中信息: %d",
			clist->group_selected_row+1,clist->show_selected_row+1);
	context_id=gtk_statusbar_get_context_id(GTK_STATUSBAR(clist->statusbar),"");
	gtk_statusbar_push(GTK_STATUSBAR(clist->statusbar),context_id,text);
	#ifdef _DEBUG_
		printf("current show selected:(%d,%d)\n",clist->show_selected_row,
									clist->show_selected_column);
	#endif
}					

                                    
gboolean on_clist_show_button_press_event(GtkWidget *widget,
                                        GdkEventButton  *event,gpointer         user_data)
{
         GtkWidget *popup_menu_for_clist_show;
  
	if(event->button==BUTTON_RIGHT){
		popup_menu_for_clist_show= (GtkWidget *)create_popup_menu_for_clist_show();
		gtk_menu_popup (GTK_MENU(popup_menu_for_clist_show), 
						NULL, NULL, NULL, NULL,event->button, event->time);	
        return TRUE;
    }
    return FALSE;
 }

gboolean
on_clist_group_button_press_event           (GtkWidget       *widget,
                                        GdkEventButton  *event,
                                        gpointer         user_data)
{	
	GtkWidget *popup_menu_for_clist_group;
  
	if(event->button==BUTTON_RIGHT){
		popup_menu_for_clist_group= (GtkWidget *)create_popup_menu_for_clist_group();
		gtk_menu_popup (GTK_MENU(popup_menu_for_clist_group), NULL, NULL, NULL, NULL,
								event->button, event->time);	
        return TRUE;
    }
    return FALSE;
}

gboolean delete_info_from_clist_show(gpointer user_data)
{
	struct newinfo_struct *newinfo=(struct newinfo_struct *)user_data;
	struct file_ctrl *global_file=newinfo->global_file;
	struct clist_struct  *clist=newinfo->clist;
	GtkWidget *dialog;
	gint sure;
	gchar info_nums[10];
	#ifdef _DEBUG_
		printf("on_item_edit_delete_info_activate:-------\n");
	#endif
	gchar msg[] = "确认删除本条信息?";
	dialog = gtk_message_dialog_new(GTK_WINDOW(window_main), 0,
			GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, msg);
	sure = gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
	if (sure == GTK_RESPONSE_OK) {
		gtk_clist_remove(GTK_CLIST(clist->clist_show),clist->show_selected_row);
		*clist->show_all_row -=1;
		g_snprintf(info_nums,10,"%d",*clist->show_all_row);
		if(clist->show_selected_row==*clist->show_all_row) {
			gtk_clist_select_row(GTK_CLIST(clist->clist_show),*clist->show_all_row-1,0);
		}
		gtk_clist_set_text(GTK_CLIST(clist->clist_group),
						   clist->group_selected_row,1,info_nums);
		global_file->cur_file->modify=TRUE;
		global_file->data_mod=TRUE;
		save_tmp_file(newinfo);
	}
}

gboolean delete_info_from_clist_group(gpointer user_data)
{
	struct newinfo_struct *newinfo=(struct newinfo_struct *)user_data;
	struct file_ctrl *global_file=newinfo->global_file;
	struct clist_struct  *clist=newinfo->clist;
	struct file_list *files,*del;
	GtkWidget *dialog;
	gint sure;
	gchar info_nums[10],tmp[80];
	#ifdef _DEBUG_
		printf("delete_info_from_clist_group:-------\n");
	#endif
	gchar msg[] = "删除一个组将删除该组的所有信息，是否删除?";
	dialog = gtk_message_dialog_new(GTK_WINDOW(window_main), 0,
			GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, msg);
	sure = gtk_dialog_run(GTK_DIALOG(dialog));
	gtk_widget_destroy(dialog);
	if (sure == GTK_RESPONSE_OK) {
		gtk_clist_remove(GTK_CLIST(clist->clist_group),clist->group_selected_row);
		*clist->group_all_row -=1;
		for(files=global_file->files;files->next->next != NULL;) {
			if(strcmp(files->next->name,global_file->cur_file->name) == 0)
				break;
			files=files->next;
		}
		del=files->next;
		files->next=del->next;
		global_file->modify=TRUE;
		strcpy(global_file->current,global_file->files->name);
		save_config_file(global_file);
		g_snprintf(tmp,80,"%s/%s",global_file->data,global_file->files->name);
		unlink(tmp);
		if(global_file->data_mod) {
			for(files=global_file->files;files->next!= NULL;) {
				if(files->modify)
					break;
				files=files->next;
			}
			if(files->next == NULL)
				global_file->data_mod=FALSE;
		}
		gtk_clist_select_row(GTK_CLIST(clist->clist_group),0,0);
		gtk_clist_select_row(GTK_CLIST(clist->clist_show),0,0);
	}
	return TRUE;
}
